//
//  MqForumViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqForumViewController.h"
#import "MqForumChoiceViewController.h"
#import "MqForumCommonViewController.h"
#import "MqForumPostViewController.h"
#import <MBProgressHUD.h>
@interface MqForumViewController ()
<UIScrollViewDelegate>
//  精华热帖
@property (nonatomic, strong) UIButton *choiceButton;
//  常用版块
@property (nonatomic, strong) UIButton *commonButton;
//  发帖
@property (nonatomic, strong) UIButton *postButton;

@property (nonatomic, strong) UIScrollView *scrollView;

//  红色滑动条
@property (nonatomic, strong) UIImageView *redStripes;

@property (nonatomic, strong) MqForumCommonViewController *commonVC;



@property (nonatomic, assign) CGPoint p;
@end

@implementation MqForumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addButtons];
    
    [self addControllers];
    
}

- (void)addButtons{
    
    //  精华热帖
    _choiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _choiceButton.frame = CGRectMake(10, 30, kScreenWidth / 5 + 10, 20);
    [_choiceButton setTitle:@"精选热帖" forState:(UIControlStateNormal)];
    [_choiceButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [_choiceButton setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)];
    [_choiceButton addTarget:self action:@selector(choiceButton:) forControlEvents:(UIControlEventTouchUpInside)];
    _choiceButton.selected = YES;
    [self.view addSubview:_choiceButton];
    
    //  红色条
    _redStripes = [[UIImageView alloc] initWithFrame:CGRectMake(_choiceButton.left, 60, _choiceButton.width, 4)];
    _redStripes.image = [UIImage imageNamed:@"icon_credits_redPromote@2x"];
    [self.view addSubview:_redStripes];

    //  常用版块
    _commonButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _commonButton.frame = CGRectMake(_choiceButton.right + 10, 30, kScreenWidth / 5 + 10, 20);
    [_commonButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [_commonButton setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)];
    [_commonButton setTitle:@"常用版块" forState:(UIControlStateNormal)];
    [_commonButton addTarget:self action:@selector(commonButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_commonButton];
    
    //  发帖
    _postButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _postButton.frame = CGRectMake(kScreenWidth - 60, 30, 60, 20);
    [_postButton setTitle:@"发帖" forState:(UIControlStateNormal)];
    [_postButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [_postButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [_postButton addTarget:self action:@selector(postButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_postButton];
    
}

//  精选热帖的点击方法
- (void)choiceButton:(UIButton *)button{
    _choiceButton.selected = YES;
    _commonButton.selected = NO;
    [self redStripesMove];
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
}

//  常用版块
- (void)commonButton:(UIButton *)button{
    
    [_commonVC.tableView reloadData];
    _commonButton.selected = YES;
    _choiceButton.selected = NO;
    [self redStripesMove];
    [_scrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];

}

//  发帖按钮
- (void)postButton:(UIButton *)button{
    
    MqForumPostViewController *postVC = [[MqForumPostViewController alloc] init];
    
    [self.navigationController pushViewController:postVC animated:YES];
    
}

//  红色滑动条移动
- (void)redStripesMove{
    
    if (_choiceButton.selected == YES) {
        
        CGRect newFrame = CGRectMake(_choiceButton.left, 60, _commonButton.width, 4);
        
        [UIView animateWithDuration:0.2 animations:^{
           
            _redStripes.frame = newFrame;
            
        }];
        
        
    }else{
        
        CGRect newFrame = CGRectMake(_commonButton.left, 60, _commonButton.width, 4);
        
        [UIView animateWithDuration:0.2 animations:^{
           
            _redStripes.frame = newFrame;
            
        }];
        
    }
    
}


//  添加控制器
- (void)addControllers{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenHeight - 64);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    //  精华热帖
    MqForumChoiceViewController *choiceVC = [[MqForumChoiceViewController alloc] init];
    choiceVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
//    choiceVC.view.backgroundColor = [UIColor yellowColor];
    [self addChildViewController:choiceVC];
    [_scrollView addSubview:choiceVC.view];
    
    //  常用版块
    _commonVC = [[MqForumCommonViewController alloc] init];
//    _commonVC.view.backgroundColor = [UIColor blueColor];
    _commonVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 64);
    [self addChildViewController:_commonVC];
    [_scrollView addSubview:_commonVC.view];
    
}


#pragma mark --- scrollView代理方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.p = scrollView.contentOffset;
    NSLog(@"%f",_p.x);
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView{
    if ( scrollView.contentOffset.x - self.p.x > kScreenWidth / 2) {
        
        NSLog(@"pianyi :%f",scrollView.contentOffset.x);
        [self commonButton:nil];
        
    }
    
}

//  视图将要出现时执行的方法
- (void)viewDidAppear:(BOOL)animated{
    
     self.tabBarController.tabBar.hidden = NO;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
